/*
//  Copyright © 2014 Allen County Public Library.

  //        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
 * Author: James Harpe
 * 
 * Description: This class handles web service calls associated with hold cancellation.
 * 
 */

package us.in.lib.acpl.Mobile_ACPL.ws;

import java.util.List;

import com.google.analytics.tracking.android.EasyTracker;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.view.View;
import us.in.lib.acpl.Mobile_ACPL.AccountHoldsFragment;
import us.in.lib.acpl.Mobile_ACPL.R;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import us.in.lib.acpl.Mobile_ACPL.patron.Hold;
import us.in.lib.acpl.Mobile_ACPL.util.RestClient;
import us.in.lib.acpl.Mobile_ACPL.util.RestClient.RequestMethod;

public class CancelHoldsHandler {
	public static final String CANCEL_HOLD_URL = Mobile_ACPL.WS_HOST+"/symws/rest/patron/cancelMyHold";
	private List<Hold> toCancel;
	private AccountHoldsFragment frag;
	SharedPreferences prefs;

	public CancelHoldsHandler(AccountHoldsFragment frag) {

		this.frag = frag;
	}

	public void cancel(List<Hold> toCancel) {
		this.toCancel = toCancel;
		prefs = frag.getActivity().getSharedPreferences("myPrefs",
				Context.MODE_WORLD_WRITEABLE);
		LoginHandler loginHandler = new LoginHandler();
		loginHandler.getNewSessionToken(prefs, frag.getActivity());

		if (prefs.getString("sessionToken", null) != null) {
			new CancelHoldsTask().execute();
		} else {
			frag.getActivity().findViewById(R.id.accountBar)
					.setVisibility(View.GONE);

		}

	}

	private class CancelHoldsTask extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected Boolean doInBackground(Void... arg0) {

			String sessionToken = prefs.getString("sessionToken", null);

			boolean success = true;

			for (int i = 0; i < toCancel.size(); i++) {
				Hold currHold = toCancel.get(i);
				String key = currHold.getHoldKey();

				RestClient client = new RestClient(CANCEL_HOLD_URL);
				client.AddHeader("x-sirs-clientID",
						SearchCatalogHandler.CLIENT_ID);
				client.AddParam("sessionToken", sessionToken);
				client.AddParam("holdKey", key);

				try {
					client.Execute(RequestMethod.POST);
				} catch (Exception e) {
					e.printStackTrace();
				}

				String response = client.getResponse();

				if (!response.startsWith("true")) {
					success = false;
					EasyTracker.getTracker().sendEvent("Holds", "Hold Cancellation Failed", null, null);
				}else{
					EasyTracker.getTracker().sendEvent("Holds", "Hold Cancelled", null, null);
				}

			}
			return Boolean.valueOf(success);

		}

		@Override
		protected void onPostExecute(Boolean success) {
			frag.handleCancellationResult(success.booleanValue(), toCancel);

		}
	}
}
